package com.xbniao.owms.utils;

import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;

/**
 * 类说明
 * @author wangbin
 */
public class StringUtil {
	public static final String BY_SYMBOL_WORD_SEGMENTATION = " _";

	/**
	 * 判断表示是否全为英文  
	 * @param word
	 * @return
	 */
    public static boolean strIsEnglish(String word) {  
    	char temp = ' ';
        for (int i = 0; i < word.length(); i++) {  
            if (!(word.charAt(i) >= 'A' && word.charAt(i) <= 'Z')  
                    && !(word.charAt(i) >= 'a' && word.charAt(i) <= 'z') 
                    && !(word.charAt(i)==temp)
                    && !(word.charAt(i)=='_')) {  
                return false;  
            }  
        }  
        return true;  
    }
	
	/**
	 * 将文本中的字符串按符号分成若干单词
	 * @param str
	 * @return
	 */
	public static List<String> bySymbolWordSegmentation(String str)
	{
		List<String> list = new ArrayList<String>();
		StringTokenizer st = new StringTokenizer(str, BY_SYMBOL_WORD_SEGMENTATION);
		if(st != null)
		{
			while (st.hasMoreTokens()) {
				String key = st.nextToken();
				list.add(key);
			}
		}
		return list;
	}
	
	/**
	 * 按某个字符分隔字符串成list
	 * @param str
	 * @param split
	 * @return
	 */
	public static List<String> stringToList(String str,String split)
	{
		List<String> list = new ArrayList<String>();
		String[] temp = str.split(split);
		if(temp != null && temp.length > 0)
		{
			for(String tep : temp)
				list.add(tep);
		}
		return list;
	}
	
	/**
	 * 检测某个字符串是否符合要求
	 * @param srcString      要拆分的字符串（默认按照英文逗号分隔）
	 * @param be_detected    被检测的字符串
	 * @return           -1  :  申报名称只能是英文字符，空格，英文下划线
	 * 					  0  :  包含敏感词
	 * 					  1  :  通过验证
	 */
	public static int checkName(String srcString, String be_detected)
	{
		int flag = 1;
		
		if(!strIsEnglish(be_detected))
			return -1;
		List<String> srcList = stringToList(srcString, ",");
		List<String> checkList = bySymbolWordSegmentation(be_detected);
		if(srcList.size() > 0 && checkList.size() > 0)
		{
			for(String check : checkList)
			{
				for(String src : srcList)
				{
					if(src.equals(check))
					{
						flag = 0;
						break;
					}	
				}
				if(flag != 1)
					break;
			}
		}
		return flag;
	}
	
	/**
	 * 将追溯码转换成10位数字标识
	 * @param traceCode
	 * @return
	 */
	public static String hexToInteger(String traceCode)
	{
		if(StringUtils.isEmpty(traceCode))
			return "";
		String hexnum = traceCode.substring(12, 20);
		String intnum = Integer.parseInt(hexnum, 16)+"";
		int numsize = intnum.length();
		String returnStr = "";
		for(int i = 0 ; i < 10-numsize ; i++)
			returnStr += "0";
		returnStr += intnum;
		return returnStr;
	}
	
	public static void main(String[] args) {
		String str ="baby,boy,girl,dog";
		String cheString = "aaaaa";
		System.out.println(StringUtil.checkName(str, cheString));
	}
}
